package com.gui.listeners;

import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

/**
 * CustomerTableModelListener is a custom table model listener for the
 * customerTable component.
 * @author sithum
 */
public class CustomerTableModelListener implements TableModelListener {

    private JTextField name;
    private JTextField shop;
    private JTextField telephone;
    private JTextField mobile;
    private JTextField discountField;
    private JComboBox route;
    private JComboBox discount;
    private JTextArea address;
    private JTable table;

    public CustomerTableModelListener(JTable table) {
        this.table = table;
        table.getModel().addTableModelListener(this);
    }

    public void tableChanged(TableModelEvent e) {
        System.out.println("In customer table model listener.");
        int selectedRow = e.getFirstRow();
        int selectedColumn = e.getColumn();
        Object changedValue = table.getValueAt(selectedRow, selectedColumn);
        if (changedValue.toString().equalsIgnoreCase("true")) {
            name.setText(table.getValueAt(selectedRow, 1).toString());
            shop.setText(table.getValueAt(selectedRow, 2).toString());
            telephone.setText(table.getValueAt(selectedRow, 3).toString());
            mobile.setText(table.getValueAt(selectedRow, 4).toString());
            address.setText(table.getValueAt(selectedRow, 5).toString());
            route.setSelectedItem(table.getValueAt(selectedRow, 6));
            Object discountAmount = table.getValueAt(selectedRow, 7);
            Object discountPercentage = table.getValueAt(selectedRow, 8);
            if (discountAmount != null) {
                discountField.setText(discountAmount.toString());
                discount.setSelectedItem(new String("Amount (Rs.)"));
            } else {
                discountField.setText(discountPercentage.toString());
                discount.setSelectedItem(new String("Percentage (%)"));
            }

        } else if (changedValue.toString().equalsIgnoreCase("false")) {
            name.setText("");
        }
    }

    /**
     * @return the name
     */
    public JTextField getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(JTextField name) {
        this.name = name;
    }

    /**
     * @return the shop
     */
    public JTextField getShop() {
        return shop;
    }

    /**
     * @param shop the shop to set
     */
    public void setShop(JTextField shop) {
        this.shop = shop;
    }

    /**
     * @return the telephone
     */
    public JTextField getTelephone() {
        return telephone;
    }

    /**
     * @param telephone the telephone to set
     */
    public void setTelephone(JTextField telephone) {
        this.telephone = telephone;
    }

    /**
     * @return the mobile
     */
    public JTextField getMobile() {
        return mobile;
    }

    /**
     * @param mobile the mobile to set
     */
    public void setMobile(JTextField mobile) {
        this.mobile = mobile;
    }

    /**
     * @return the discountField
     */
    public JTextField getDiscountField() {
        return discountField;
    }

    /**
     * @param discountField the discountField to set
     */
    public void setDiscountField(JTextField discountField) {
        this.discountField = discountField;
    }

    /**
     * @return the route
     */
    public JComboBox getRoute() {
        return route;
    }

    /**
     * @param route the route to set
     */
    public void setRoute(JComboBox route) {
        this.route = route;
    }

    /**
     * @return the discount
     */
    public JComboBox getDiscount() {
        return discount;
    }

    /**
     * @param discount the discount to set
     */
    public void setDiscount(JComboBox discount) {
        this.discount = discount;
    }

    /**
     * @return the address
     */
    public JTextArea getAddress() {
        return address;
    }

    /**
     * @param address the address to set
     */
    public void setAddress(JTextArea address) {
        this.address = address;
    }
}
